package com.template;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.template.core.AttrBean;
import com.template.core.ColumnBean;
import com.template.core.DbConnection;
import com.template.core.ExportedKeyBean;
import com.template.core.ImportedKeyBean;
import com.template.core.PrimaryBean;
import com.template.core.TableBean;
import com.template.core.TemplateHandler;

public class CreateMain {

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) {
		TemplateHandler temp = new TemplateHandler(
				"E:/project/eclipse/template/src/com/template/template");
		DbConnection db = DbConnection.getInstance();
		List<TableBean> tbs = db.tables();
		for (TableBean tb : tbs) {
			createPojoAndMapping(temp, tb); // pojo,mapping
			createVo(temp, tb); // vo
			createDao(temp, tb); // 创建DAO
			createAction(temp, tb); // 创建Action
			createService(temp, tb); // 创建Service
			createPage(temp, tb); // 创建jsp
		}
	}
	
	public static void createPage(TemplateHandler temp, TableBean tb) {
		List<AttrBean> list = new ArrayList<AttrBean>();
		List<String> packageList = new ArrayList<String>();
		Map<String, Object> param = new HashMap<String, Object>();
		String packageName = tb.getTableName().replace("t_", "");
		String className = packageName.replaceFirst(
				packageName.substring(0, 1), packageName.substring(0, 1)
						.toUpperCase());
		String savepath = "d:/src/pages/" + packageName;
		File file = new File(savepath);
		if(!file.exists()) {
			file.mkdirs();
		}
		String listpath = savepath + "/list.jsp";
		String editorpath = savepath + "/editor.jsp";
		param.put("packageName", packageName);
		param.put("className", className);
		param.put("imports", packageList);
		param.put("attrs", list);
		List<ColumnBean> cbs =  tb.getColumns();
		for (ColumnBean cb : cbs) {
			String _column = cb.getColumnName();
			String _type = cb.getType();
			String s = getType(_type, packageList);
			list.add(new AttrBean(_column, s));
		}
		List<PrimaryBean> pbs = tb.getPrimarys(); // 主键
		for (PrimaryBean pb : pbs) {
			String _c = pb.getColumnName();
			param.put("p_column", _c);
		}
		try {
			temp.execute("list.ftl", listpath, param);
			temp.execute("editor.ftl", editorpath, param);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void createDao(TemplateHandler temp, TableBean tb) {
		Map<String, Object> param = new HashMap<String, Object>();
		String packageName = tb.getTableName().replace("t_", "");
		String className = packageName.replaceFirst(
				packageName.substring(0, 1), packageName.substring(0, 1)
						.toUpperCase());
		String savepath = "d:/src/com/naire/modul/" + packageName + "/dao/impl";
		File file = new File(savepath);
		if(!file.exists()) {
			file.mkdirs();
		}
		List<String> list = new ArrayList<String>();
		String daopath = "d:/src/com/naire/modul/" + packageName + "/dao" + "/" + className + "Dao.java";
		String daoimplpath = "d:/src/com/naire/modul/" + packageName + "/dao" + "/impl/" + className + "DaoImpl.java";
		param.put("className", className);
		param.put("packageName", packageName);
		List<PrimaryBean> pbs = tb.getPrimarys(); // 主键
		for (PrimaryBean pb : pbs) {
			String _c = pb.getColumnName();
			String id = _c.replaceFirst(
					_c.substring(0, 1), _c.substring(0, 1)
					.toUpperCase());
			param.put("p_column", id);
			param.put("p_type", getType(pb.getType(), list));
		}
		try {
			temp.execute("dao_java.ftl", daopath, param);
			temp.execute("daoimpl_java.ftl", daoimplpath, param);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void createAction(TemplateHandler temp, TableBean tb) {
		Map<String, Object> param = new HashMap<String, Object>();
		String packageName = tb.getTableName().replace("t_", "");
		String className = packageName.replaceFirst(
				packageName.substring(0, 1), packageName.substring(0, 1)
						.toUpperCase());
		String savepath = "d:/src/com/naire/modul/" + packageName + "/action";
		File file = new File(savepath);
		if(!file.exists()) {
			file.mkdirs();
		}
		List<String> list = new ArrayList<String>();
		String actionpath = "d:/src/com/naire/modul/" + packageName + "/action/" + className + "Action.java";
		String actionconfigpath = "d:/src/com/naire/modul/" + packageName + "/action/struts-" + packageName + ".xml";
		param.put("className", className);
		param.put("packageName", packageName);
		List<PrimaryBean> pbs = tb.getPrimarys(); // 主键
		for (PrimaryBean pb : pbs) {
			String _c = pb.getColumnName();
			String id = _c.replaceFirst(
					_c.substring(0, 1), _c.substring(0, 1)
					.toUpperCase());
			param.put("p_column", id);
			param.put("p_type", getType(pb.getType(), list));
		}
		try {
			temp.execute("action_java.ftl", actionpath, param);
			temp.execute("action_struts.ftl", actionconfigpath, param);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void createService(TemplateHandler temp, TableBean tb) {
		Map<String, Object> param = new HashMap<String, Object>();
		String packageName = tb.getTableName().replace("t_", "");
		String className = packageName.replaceFirst(
				packageName.substring(0, 1), packageName.substring(0, 1)
						.toUpperCase());
		String savepath = "d:/src/com/naire/modul/" + packageName + "/service";
		File file = new File(savepath);
		if(!file.exists()) {
			file.mkdirs();
		}
		List<String> list = new ArrayList<String>();
		String searchpath = "d:/src/com/naire/modul/" + packageName + "/service/" + className + "SearchService.java";
		String editorpath = "d:/src/com/naire/modul/" + packageName + "/service/" + className + "EditorService.java";
		String findpath = "d:/src/com/naire/modul/" + packageName + "/service/" + className + "FindService.java";
		String deletepath = "d:/src/com/naire/modul/" + packageName + "/service/" + className + "DeleteService.java";
		param.put("className", className);
		param.put("packageName", packageName);
		List<PrimaryBean> pbs = tb.getPrimarys(); // 主键
		for (PrimaryBean pb : pbs) {
			String _c = pb.getColumnName();
			String id = _c.replaceFirst(
					_c.substring(0, 1), _c.substring(0, 1)
					.toUpperCase());
			param.put("p_column", id);
			param.put("p_type", getType(pb.getType(), list));
		}
		try {
			temp.execute("SearchService.ftl", searchpath, param);
			temp.execute("FindService.ftl", findpath, param);
			temp.execute("EditorService.ftl", editorpath, param);
			temp.execute("DeleteService.ftl", deletepath, param);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	

	public static void createPojoAndMapping(TemplateHandler temp, TableBean tb) {
		List<AttrBean> p_list = new ArrayList<AttrBean>();
		List<AttrBean> fk_list = new ArrayList<AttrBean>();
		List<AttrBean> pk_list = new ArrayList<AttrBean>();
		List<String> packageList = new ArrayList<String>();
		Map<String, Object> param = new HashMap<String, Object>();
		String packageName = tb.getTableName().replace("t_", "");
		String className = packageName.replaceFirst(
				packageName.substring(0, 1), packageName.substring(0, 1)
						.toUpperCase());
		String savepath = "d:/src/com/naire/modul/" + packageName + "/pojo";
		String _savepath = savepath + "/" + className + ".hbm.xml";
		File file = new File(savepath);
		if(!file.exists()) {
			file.mkdirs();
		}
		savepath += "/" + className + ".java";
		param.put("packageName", packageName);
		param.put("tableName", tb.getTableName());
		param.put("className", className);
		param.put("imports", packageList);
		param.put("attrs", p_list);
		param.put("attrs_fk", fk_list);
		param.put("attrs_pk", pk_list);
		
		List<String> addColumn = new ArrayList<String>();
		
		List<ExportedKeyBean> ekbs = tb.getExportedKeys(); //外键
		for (ExportedKeyBean ekb : ekbs) {
			String _table = ekb.getFkTableName().replace("t_", "");
			String _className = _table.replaceFirst(_table.substring(0, 1),
					_table.substring(0, 1).toUpperCase());
			String _import = "com.naire.modul." + _table + ".pojo."
					+ _className;
			if (!packageList.contains("java.util.Set")) {
				packageList.add("java.util.Set");
				packageList.add("java.util.HashSet");
			}
			if(!packageList.contains(_import)) {				
				packageList.add(_import);
			}
			AttrBean ab = new AttrBean(_table, "Set");
			ab.setTableName(ekb.getFkTableName());
			ab.setColumnName(ekb.getFkColumnName());
			ab.setClassName(_import);
			fk_list.add(ab);
			addColumn.add(ekb.getFkColumnName());
		}
		
		List<ImportedKeyBean> ikbs = tb.getImportedKeys(); // 主键
		for (ImportedKeyBean ikb : ikbs) {
			String _table = ikb.getPkTableName().replace("t_", "");
			String _className = _table.replaceFirst(_table.substring(0, 1),
					_table.substring(0, 1).toUpperCase());
			String _import = "com.naire.modul." + _table + ".pojo."
					+ _className;
			if(!packageList.contains(_import)) {				
				packageList.add(_import);
			}
			AttrBean ab = new AttrBean(_table, _className);
			ab.setColumnName(ikb.getPkColumnName());
			ab.setClassName(_import);
			pk_list.add(ab);
			addColumn.add(ikb.getPkColumnName());
		}
		
		List<PrimaryBean> pbs = tb.getPrimarys(); // 主键
		for (PrimaryBean pb : pbs) {
			String p = pb.getColumnName();
			String n = p.replaceFirst(p.substring(0, 1),
					p.substring(0, 1).toUpperCase());
			param.put("p_column", p);
			param.put("b_column", n);
			param.put("p_type", getType(pb.getType(), packageList));
//			AttrBean ab = new AttrBean(pb.getColumnName(), getType(pb.getType(), packageList));
//			p_list.add(ab);
			addColumn.add(pb.getColumnName());
		}
		
		List<ColumnBean> cbs =  tb.getColumns();
		for (ColumnBean cb : cbs) {
			String _column = cb.getColumnName();
			String _type = cb.getType();
			String s = getType(_type, packageList);
			if(s.equals("Date")) {
				s = "java.util.Date";
			} else if(s.equals("BigDecimal")) {
				s = "java.math.BigDecimal";
			} else {
				s = "java.lang." + s;
			}
			AttrBean ab = new AttrBean(_column, s);
			if(!addColumn.contains(_column)) {				
				p_list.add(ab);
			}
		}
		try {
			temp.execute("pojo_java.ftl", savepath, param);
			temp.execute("pojo_mapping.ftl", _savepath, param);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void createVo(TemplateHandler temp, TableBean tb) {
		List<AttrBean> list = new ArrayList<AttrBean>();
		List<String> packageList = new ArrayList<String>();
		Map<String, Object> param = new HashMap<String, Object>();
		String packageName = tb.getTableName().replace("t_", "");
		String className = packageName.replaceFirst(
				packageName.substring(0, 1), packageName.substring(0, 1)
						.toUpperCase());
		String savepath = "d:/src/com/naire/modul/" + packageName + "/vo";
		File file = new File(savepath);
		if(!file.exists()) {
			file.mkdirs();
		}
		savepath += "/" + className + "Vo.java";
		param.put("packageName", packageName);
		param.put("className", className);
		param.put("imports", packageList);
		param.put("attrs", list);
		List<ColumnBean> cbs =  tb.getColumns();
		for (ColumnBean cb : cbs) {
			String _column = cb.getColumnName();
			String _type = cb.getType();
			String s = getType(_type, packageList);
			
			list.add(new AttrBean(_column, s));
		}
		try {
			temp.execute("vo_java.ftl", savepath, param);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static String getType(String t, List<String> packageList) {
		if(t.equalsIgnoreCase("INT")) {
			return "Integer";
		} else if(t.equalsIgnoreCase("DATE")) {
			if(!packageList.contains("java.util.Date")) {
				packageList.add("java.util.Date");
			}
			return "Date";
		} else if(t.equalsIgnoreCase("DOUBLE") || t.equalsIgnoreCase("FLOAT") || t.equalsIgnoreCase("DECIMAL")) {
			if(!packageList.contains("java.math.BigDecimal")) {
				packageList.add("java.math.BigDecimal");
			}
			return "BigDecimal";
		} else {
			return "String";
		}
	}
}
